package com.wonders.qxmanager.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.qxmanager.base.BaseDao;
import com.wonders.qxmanager.base.Parameter;
import com.wonders.qxmanager.base.WidgetPage;
import com.wonders.qxmanager.dao.LogDao;
import com.wonders.qxmanager.model.Log;
import com.wonders.qxmanager.model.User;
import com.wonders.qxmanager.utils.PageArrayList;

@Repository
public class LogDaoImpl extends BaseDao<Log, Serializable> implements LogDao {

	public void addLog(Log log) {
		save(log);

	}

	public void updateLog(Log log) {
		update(log);
	}

	public void deleteLog(BigDecimal id) {
		deleteById(id);
	}

	public PageArrayList<Log> getListByPage(Log log, WidgetPage page) {
		Criteria criteria = getSession().createCriteria(Log.class);
		if (log.getId() != null)
			criteria.add(Restrictions.like("id", "%" + log.getId() + "%"));
		if (log.getWhat() != null)
			criteria.add(Restrictions.like("what", "%" + log.getWhat() + "%"));

		int total = ((Long) criteria.setProjection(Projections.rowCount())
				.uniqueResult()).intValue();
		criteria.setResultTransformer(Criteria.ROOT_ENTITY);
		criteria.setProjection(null);
		int startIndex = page.getStartOfPage();
		criteria.setFirstResult(startIndex - 1);
		criteria.setMaxResults(page.getPageSize());
		List<Log> list = criteria.list();
		PageArrayList<Log> pageList = new PageArrayList<Log>(total,
				page.getPageSize(), page.getCurrentPage());
		pageList.addAll(list);
		return pageList;
	}

	public List<Log> getAll() {
		return getListByHQL("from Log", null);
	}

	public Log getLogById(Integer id) {
		return get(id);
	}

	public List<Log> getList(String sqlString, Parameter parameter) {
		return getListBySQL(sqlString, parameter);
	}

}
